//
//  STStatusVM.m
//  OCVVeibo
//
//  Created by leergou on 16/7/27.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

#import "STStatusVM.h"
#import "STStatusDTO.h"


@interface STStatusVM ()

@end

@implementation STStatusVM

// 微博时间


// 自定义构造方法
- (instancetype)initWithStatus:(STStatusDTO *)status{
    
    if (self = [super init]) {
        // 对自身的 status 属性赋值
        self.status = status;
        self.verifiedImage = [self getVerifiedImage:status.user.verified.integerValue];
//        NSLog(@"%@",status.user.verified);
        self.vipImage = [self getVipImage:status.user.mbrank.integerValue];
        self.repostsTitle = [self getCount:status.reposts_count.integerValue title:@"转发"];
        self.commentTitle = [self getCount:status.comments_count.integerValue title:@"评论"];
        self.unlikeTitle = [self getCount:status.attitudes_count.integerValue title:@"赞"];
//        NSLog(@"%@",status.source);
        self.sourceAttributeText = [self getAttributeStringWithSource:status.source];
        // 对需要处理的属性,进行处理
       /*
        // 微博来源
        @property (nonatomic,copy) NSString *sourceAttributeText;
        // 微博发布时间
        @property (nonatomic,copy) NSString *creatTimeDescription;
        */
//        status.source
    }
    return self;
}

#pragma mark - 微博来源 label 处理
// 字符串截取
- (NSMutableAttributedString *)getAttributeStringWithSource:(NSString *)source{
    
    if (!source || [source  isEqual: @""]) {
//        NSLog(@"来源为空");
        source = @"未知";
        return [self getAttributeStringWithString:source];
    }

    // 转成富文本
    NSMutableAttributedString *mutableAttrString = [self getAttributeStringWithString:[source subString:@"\">" endString:@"</"]];
    
    return mutableAttrString;
}


// 将字符串转成富文本
- (NSMutableAttributedString *)getAttributeStringWithString:(NSString *)sourceString{
    // 拼接字符串
    NSString *string = [NSString stringWithFormat:@"来自 %@",sourceString];
    
    // 创建富文本对象
    NSMutableAttributedString *mutableAttributeString = [[NSMutableAttributedString alloc] initWithString:string];
    
    // 得到 "来自" 的 range
    NSRange range = [string rangeOfString:@"来自"];
    // 设置富文本的样式
    NSDictionary *dict = @{
                           NSForegroundColorAttributeName:[UIColor darkGrayColor]
                           };
    // 设置富文本样式富文本对象
    [mutableAttributeString addAttributes:dict range:range];
    
    return mutableAttributeString;
}



#pragma mark - 转发,评论,赞按钮 title 处理
- (NSString *)getCount:(NSInteger)count title:(NSString *)title{
    if (count <= 0) {
        return title;
    } else {
        // > 0
        if (count < 10000) {
            return [NSString stringWithFormat:@"%ld",count];
        } else {
            // >= 10000
            NSString *string = [NSString stringWithFormat:@"%1f万",count/10000.0];
            if ([string containsString:@".0"]) {
                string = [string stringByReplacingOccurrencesOfString:@".0" withString:@""];
            }
            return string;
        }
    }
}

#pragma mark - 微博等级图片处理
- (UIImage *)getVipImage:(NSInteger)mbrank{
//            status.user.mbrank
    
    //common_icon_membership_level1
    if (mbrank <= 0 || mbrank >= 7) {
        return  nil;
    }
    
    if (mbrank > 0 && mbrank < 7) {
        
        NSString *imageName = [NSString stringWithFormat:@"common_icon_membership_level%ld",mbrank];
        return [UIImage imageNamed:imageName];
    }
    
    return [UIImage imageNamed:@"common_icon_membership"];
}

#pragma mark - 认证图片处理
- (UIImage *)getVerifiedImage:(NSInteger)verified{
// 处理微博认证用户图片/// 认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
//    status.user.verified
    switch (verified) {
        case 1:
            return [UIImage imageNamed:@"avatar_vip"];
            break;
        case 2:
        case 3:
        case 5:
            return [UIImage imageNamed:@"avatar_enterprise_vip"];
            break;
        case 220:
            return [UIImage imageNamed:@"avatar_grassroot"];
            break;
        default:
            return [UIImage imageNamed:@"avatar_vgirl"];
            break;
    }
}


@end
